package cn.zwxky.android.staff.Jurisdiction.Marketplace;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.staff.Jurisdiction.Technology.TechnologyTaskApproveDetailsActivity;
import cn.zwxky.android.staff.R;
import cn.zwxky.android.staff.Utils.LittleUtils;
import cn.zwxky.android.staff.Utils.URLString;


/**
 * Created by Administrator on 2017/3/31 0031.
 */

public class TaskApprove extends Fragment {

    @ViewInject(R.id.aty_setting_connect)
    private LinearLayout aty_setting_connect;

    /**
     * 点击刷新布局控件
     */
    @ViewInject(R.id.swiplayout)
    private SwipeRefreshLayout swipeRefreshLayout;

    /**
     * list列表
     */
    @ViewInject(R.id.dish_list)
    private RecyclerView dish_list;

    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;

    private List<TaskApproveListBean.ListBean> mList;

    private TaskApproveListAdapter adapter;

    /**
     * view对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_task_approve, null);
        ViewInjectUtils.inject(this, view);

        aty_setting_connect.setVisibility(View.GONE);

        mList = new ArrayList<>();
        adapter = new TaskApproveListAdapter(mactivity, mList, R.layout.item_task_approve);
        mlayoutManager = new LinearLayoutManager(mactivity);//布局模式为listview方式
        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });

        getData();
        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }

    /**
     * 审批列表
     */
    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.MANAGE_MYEAALIST, hashMap, TaskApproveListBean.class, new TaskApproveListEvent<TaskApproveListBean>(), true, true);
    }

    @EventAnnotation
    public void TaskApproveListEvent(TaskApproveListEvent<TaskApproveListBean> event) {
        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()) {
            case 200:
                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                mList.clear();
                adapter.notifyDataSetChanged();
                SN.SnackShow(dish_list, event.getMsg());
                break;
        }
    }

    /**
     * 跳转审批详情
     *
     * @param event
     */
    @EventAnnotation
    public void TaskApproveClickEvent(TaskApproveClickEvent event) {
        if (LittleUtils.nodel.equals("1")|| LittleUtils.depart.equals("5")) {
            //总经理 董事长
            startActivity(new Intent(mactivity, TaskApproveDetailsActivity.class).putExtra("id", event.getData()));
        } else {
            if (LittleUtils.depart.equals("1")) {
                //市场部
                startActivity(new Intent(mactivity, TaskApproveDetailsActivity.class).putExtra("id", event.getData()));
            } else if (LittleUtils.depart.equals("2")) {
                //技术部
                startActivity(new Intent(mactivity,TechnologyTaskApproveDetailsActivity.class).putExtra("id", event.getData()));
            }
        }

    }

    /**
     * 跳转审批详情
     *
     * @param event
     */
    @EventAnnotation
    public void TaskApproveClickEvent2(TaskApproveClickEvent2 event) {
        if (LittleUtils.nodel.equals("1")) {
            //总经理
            startActivity(new Intent(mactivity, TaskApproveDetailsActivity.class).putExtra("id", event.getData()));
        } else {
            if (LittleUtils.depart.equals("1")) {
                //市场部
                startActivity(new Intent(mactivity, TaskApproveDetailsActivity.class).putExtra("id", event.getData()));
            } else if (LittleUtils.depart.equals("2")) {
                //技术部
                startActivity(new Intent(mactivity,TechnologyTaskApproveDetailsActivity.class).putExtra("id", event.getData()));
            }
        }

    }


    @Override
    public void onResume() {
        super.onResume();
        getData();
        LsfbEvent.getInstantiation().register2(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        LsfbEvent.getInstantiation().unregister2(this);
    }
}
